package org.lifxue.jqda.data;

import java.awt.Point;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lifxue.jqda.util.GlobalConstant;
import org.lifxue.jqda.util.GlobalVar;

/**
 * 
 * @Description:
 * @Author: zhangzuoqiang
 * @Date: Sep 26, 2011
 */
public class DataUtil {

	private static Log logger = LogFactory.getLog(DataUtil.class);

	public static Point getPointFromHashMap() {
		Point point = new Point(0, 0);
		Iterator<?> i = GlobalVar.CONFIG.entrySet().iterator();
		while (i.hasNext()) {
			@SuppressWarnings("unchecked")
			Map.Entry<String, String> entry = (Entry<String, String>) i.next();
			String key = (String) entry.getKey(); // 返回与此项对应的键
			String value = (String) entry.getValue(); // 返回与此项对应的值
			if (key.equals(GlobalConstant.CONFIG_FILE_X)) {
				point.x = Integer.parseInt(value);
			}
			if (key.equals(GlobalConstant.CONFIG_FILE_Y)) {
				point.y = Integer.parseInt(value);
			}
		}
		return point;
	}

	public static void updateConfigFile() {
		try {
			File file = new File(GlobalVar.USER_HOME
					+ GlobalVar.FILE_PATH_DELIMITER
					+ GlobalConstant.DEFAULT_CONFIG_DIR
					+ GlobalVar.FILE_PATH_DELIMITER
					+ GlobalConstant.CONFIG_FILE);
			if (file.isFile() && file.exists()) {
				file.delete();
				new XmlData().writeConfigXml(file, GlobalVar.CONFIG,
						GlobalVar.HOTKEY);
			}
		} catch (Exception ex) {
			logger.debug(ex);
		}
	}

	public static void updateAppsFile() {
		try {
			File file = new File(GlobalVar.USER_HOME
					+ GlobalVar.FILE_PATH_DELIMITER
					+ GlobalConstant.DEFAULT_CONFIG_DIR
					+ GlobalVar.FILE_PATH_DELIMITER + GlobalConstant.APPS_FILE);
			if (file.isFile() && file.exists()) {
				file.delete();
				new XmlData().writeAppsXml(file, GlobalVar.APPLICATIONS,
						GlobalVar.APP_SCAN_DIR);
			}
		} catch (Exception ex) {
			logger.debug(ex);
		}

	}

	public static void updateIndexFile() {
		try {
			File file = new File(GlobalVar.USER_HOME
					+ GlobalVar.FILE_PATH_DELIMITER
					+ GlobalConstant.DEFAULT_CONFIG_DIR
					+ GlobalVar.FILE_PATH_DELIMITER + GlobalConstant.INDEX_FILE);
			if (file.isFile() && file.exists()) {
				file.delete();
				new XmlData().writeIndexXml(file, GlobalVar.INDEX_SCAN_DIR,
						GlobalVar.INDEX_SCAN_TYPE, GlobalVar.INDEX_CYCLE,
						GlobalVar.INDEX_LASTTIME);
			}
		} catch (Exception ex) {
			logger.debug(ex);
		}
	}

}
